/**
 * @Function 调用远程服务
 * @ProjectName credit-data-platform
 * @PackageName com.py.credit.service.impl
 * @FileName LhjcRemoteBaseServiceImpl.java
 * @Date 2018年5月16日上午10:59:42
 * @Author Min DongXv
 * @Copyright (c) 2018, 金电联行 All Rights Reserved.
 */
package com.py.credit.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.constant.ApiPath;
import com.py.credit.entity.DictEntity;
import com.py.credit.entity.MsgContentEntity;
import com.py.credit.entity.SysUser;
import com.py.credit.helper.SendSysMsgHelper;
import com.py.credit.service.LhjcRemoteBaseService;
import com.py.framework.core.entity.SysAreaEntity;
import com.py.framework.core.entity.SysOfficeEntity;
import com.py.framework.core.helper.UrlHelper;
import com.py.framework.core.http.HttpAgent;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;

/**
 * @Function 调用远程服务
 * @ClassName LhjcRemoteBaseServiceImpl
 * @Date 2018年5月16日 上午10:59:42
 * @Author Min DongXv
 */
@Service
public class LhjcRemoteBaseServiceImpl implements LhjcRemoteBaseService {

    @Autowired
    private UrlHelper urlHelper;

    /**
     * @return
     * @Function 获取当前用户信息
     * @FileName LhjcRemoteBaseServiceImpl.java
     * @MethodName getCurrentUser
     * @Date 2018年5月19日 下午4:44:59
     * @Author Min DongXv
     */
    @Override
    public RestServiceResult<SysUser> getCurrentUser(String jdToken) {
        String url = urlHelper.getBaseServerUrl() + ApiPath.GET_CURRENT_USER;
        Map<String, String> params = new HashMap<String, String>(1);
        String result = "";
        try {
            // 发送请求
            result = HttpAgent.getInstance().sendHttpPost(url, params, jdToken);
            return JSONObject.parseObject(result, new TypeReference<RestServiceResult<SysUser>>() {
            });
        } catch (Exception e) {
            RestServiceResult<SysUser> userData = new RestServiceResult<SysUser>();
            userData.setCode(RestServiceResult.CODE_UNKNOW);
            userData.setMsg("系统异常，请稍后再试！");
            ExceptionLogger.error(
                    "远程服务-获取当前用户信息调用异常！(LhjcRemoteBaseService#getCurrentUser) URL:" + url + "Result:" + result,
                    e.getMessage(), e);
            return userData;
        }
    }

    @Override
    public List<Long> getDeptManager(String deptIdList, String jdToken) {
        String url = urlHelper.getBaseServerUrl() + ApiPath.GET_DEPT_MANAGER_URL;
        Map<String, String> params = new HashMap<String, String>(1);
        params.put("deptIds", deptIdList);
        String result = "";
        try {
            // 发送请求
            result = HttpAgent.getInstance().sendHttpPost(url, params, jdToken);
            return JSONObject.parseObject(result, new TypeReference<RestServiceResult<List<Long>>>() {
            }).getData();
        } catch (Exception e) {
            RestServiceResult<List<Long>> data = new RestServiceResult<List<Long>>();
            data.setCode(RestServiceResult.CODE_UNKNOW);
            data.setMsg("系统异常，请稍后再试！");
            ExceptionLogger.error(
                    "远程服务-获取当前用户信息调用异常！(LhjcRemoteBaseService#getDeptManager) URL:" + url + "Result:" + result,
                    e.getMessage(), e);
            return data.getData();
        }
    }

    @Override
    public RestServiceResult<List<DictEntity>> getDictByDictKey(String dictKey) {
        String url = urlHelper.getBaseServerUrl() + ApiPath.GET_DICT_DETAILS;
        Map<String, String> params = new HashMap<String, String>(1);
        params.put("dictKey", dictKey);
        String result = "";
        try {
            // 发送请求
            result = HttpAgent.getInstance().sendHttpPost(url, params, null);
            return JSONObject.parseObject(result, new TypeReference<RestServiceResult<List<DictEntity>>>() {
            });
        } catch (Exception e) {
            RestServiceResult<List<DictEntity>> data = new RestServiceResult<List<DictEntity>>();
            data.setCode(RestServiceResult.CODE_UNKNOW);
            data.setMsg("系统异常，请稍后再试！");
            ExceptionLogger.error("远程服务-通过字典主表ID获取字典信息调用异常！(LhjcRemoteBaseServiceImpl#getDictByDictKey) URL:" + url + " Params:"
                    + params.toString() + "Result:" + result, e.getMessage(), e);
            return data;
        }
    }

    @Override
    public RestServiceResult<DictEntity> getDictByDtlKey(String dictKey, String dtlKey) {
        String url = urlHelper.getBaseServerUrl() + ApiPath.GET_DICT_DETAIL;
        Map<String, String> params = new HashMap<String, String>(2);
        params.put("dictKey", dictKey);
        params.put("dtlKey", dtlKey);
        String result = "";
        try {
            // 发送请求
            result = HttpAgent.getInstance().sendHttpPost(url, params, null);
            return JSONObject.parseObject(result, new TypeReference<RestServiceResult<DictEntity>>() {
            });
        } catch (Exception e) {
            RestServiceResult<DictEntity> data = new RestServiceResult<DictEntity>();
            data.setCode(RestServiceResult.CODE_UNKNOW);
            data.setMsg("系统异常，请稍后再试！");
            ExceptionLogger.error("远程服务-通过字典表ID获取特定字典表数据调用异常！(LhjcRemoteBaseServiceImpl#getDictByDtlKey) URL:" + url
                    + " Params:" + params.toString() + "Result:" + result, e.getMessage(), e);
            return data;
        }
    }

    @Override
    public RestServiceResult<List<SysAreaEntity>> getAllAreas(String areaId, String jdToken) {
        String url = urlHelper.getBaseServerUrl() + ApiPath.GET_ALL_AREAS;
        Map<String, String> params = new HashMap<String, String>(1);
        params.put("areaId", areaId);
        String result = "";
        try {
            // 发送请求
            result = HttpAgent.getInstance().sendHttpPost(url, params, jdToken);
            return JSONObject.parseObject(result, new TypeReference<RestServiceResult<List<SysAreaEntity>>>() {
            });
        } catch (Exception e) {
            RestServiceResult<List<SysAreaEntity>> officeData = new RestServiceResult<List<SysAreaEntity>>();
            officeData.setCode(RestServiceResult.CODE_UNKNOW);
            officeData.setMsg("系统异常，请稍后再试！");
            ExceptionLogger.error("远程服务-获取所有区域信息调用异常！(LhjcRemoteBaseServiceImpl#getAllAreas) URL:" + url + " Params:"
                    + params.toString() + "Result:" + result, e.getMessage(), e);
            return officeData;
        }
    }

    @Override
    public RestServiceResult<List<SysOfficeEntity>> getOfficesByAreaId(String areaId, String jdToken) {
        String url = urlHelper.getBaseServerUrl() + ApiPath.GET_OFFICES_BY_AREAID;
        // 区域ID为空获取所有的部门信息
        if (StringUtils.isEmpty(areaId)) {
            url = urlHelper.getBaseServerUrl() + ApiPath.GET_ALL_OFFICES;
        }
        Map<String, String> params = new HashMap<String, String>(1);
        params.put("areaId", areaId);
        String result = "";
        try {
            // 发送请求
            result = HttpAgent.getInstance().sendHttpPost(url, params, jdToken);
            return JSONObject.parseObject(result, new TypeReference<RestServiceResult<List<SysOfficeEntity>>>() {
            });
        } catch (Exception e) {
            RestServiceResult<List<SysOfficeEntity>> officeData = new RestServiceResult<List<SysOfficeEntity>>();
            officeData.setCode(RestServiceResult.CODE_UNKNOW);
            officeData.setMsg("系统异常，请稍后再试！");
            ExceptionLogger.error("远程服务-获取部门信息调用异常！(LhjcRemoteBaseServiceImpl#getOfficesByAreaId) URL:" + url + " Params:"
                    + params.toString() + "Result:" + result, e.getMessage(), e);
            return officeData;
        }
    }

    @Override
    public Future<HttpResponse> sendMsg(List<MsgContentEntity> list, String jdToken) {
        String params = JSON.toJSONString(list);
        Future<HttpResponse> response = null;
        try {
            response = SendSysMsgHelper.postSysMsg(jdToken, urlHelper.getBaseServerUrl(), params);
        } catch (Exception e) {
            ExceptionLogger.error("远程服务-发送站内信调用异常！(LhjcRemoteBaseServiceImpl#sendMsg) Params:" + params, e.getMessage(), e);
            return null;
        }
        return response;
    }

    @Override
    public SysUser getByUsername(String username) {
        String url = urlHelper.getBaseServerUrl() + ApiPath.GET_BY_USERNAME;
        Map<String, String> params = new HashMap<String, String>(1);
        params.put("username", username);
        String result = "";
        try {
            // 发送请求
            result = HttpAgent.getInstance().sendHttpPost(url, params, "");
            return JSONObject.parseObject(result, new TypeReference<RestServiceResult<SysUser>>() {
            }).getData();
        } catch (Exception e) {
            RestServiceResult<SysUser> data = new RestServiceResult<SysUser>();
            data.setCode(RestServiceResult.CODE_UNKNOW);
            data.setMsg("系统异常，请稍后再试！");
            ExceptionLogger.error(
                    "远程服务-根据用户名获取用户信息(巩义接口、联合奖惩接口专用)调用异常！(LhjcRemoteBaseService#getByUsername) URL:" + url + "Result:" + result,
                    e.getMessage(), e);
            return data.getData();
        }
    }

    @Override
    public String getLhjcToken(String deptId) {
        String url = urlHelper.getBaseServerUrl() + ApiPath.GET_LHJC_TOKEN;
        Map<String, String> params = new HashMap<String, String>(1);
        params.put("deptId", deptId);
        String result = "";
        try {
            // 发送请求
            result = HttpAgent.getInstance().sendHttpPost(url, params, "");
            return JSONObject.parseObject(result, new TypeReference<RestServiceResult<String>>() {
            }).getData();
        } catch (Exception e) {
            RestServiceResult<String> data = new RestServiceResult<String>();
            data.setCode(RestServiceResult.CODE_UNKNOW);
            data.setMsg("系统异常，请稍后再试！");
            ExceptionLogger.error(
                    "远程服务-获取联合奖惩部门token(联合奖惩接口专用)调用异常！(LhjcRemoteBaseService#getLhjcToken) URL:" + url + "Result:" + result,
                    e.getMessage(), e);
            return data.getData();
        }
    }
}
